﻿using BusinessDB;
using BusinessResource;
using BusinessService;
using BusinessService.proto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LightingWebService
{
    public class LightingFacade
    {
        public byte[] GetStaticResource()
        {
            OriginalBusiness os = new OriginalBusiness();
            return os.GetStaticResource();
        }

        public string SearchProdName()
        {
            OriginalBusiness os = new OriginalBusiness();
            return os.SearchProdName();
        }

        public byte[] Login(string name, string pwd)
        {
            OriginalBusiness ob = new OriginalBusiness();
            UserDB[] users = ob.GetUsers();
            if (users != null)
            {
                UserDB user = (from r in users
                               where r.Name == name && r.Pwd == pwd
                               select r).FirstOrDefault();

                if (user != null)
                {
                    UserConverter converter = new UserConverter();
                    return converter.To(user).ToByteArray();
                }
            }

            return null;
        }

        public byte[] SearchProdList(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdList(conditions);
        }

        public string SearchProdListToPDF(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdListToPDF(conditions);
        }

        public string SearchProdListToExcel(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdListToExcel(conditions);
        }

        public byte[] GetUsers()
        {
            UserArray.Builder builder = new UserArray.Builder();
            UserConverter converter = new UserConverter();

            UserService us = new UserService();
            foreach (UserDB user in us.GetItems())
            {
                builder.UserList.Add(converter.To(user));
            }
            return builder.Build().ToByteArray();
        }

        public int AddUser(byte[] bytes)
        {
            User user = User.ParseFrom(bytes);
            UserConverter converter = new UserConverter();

            OriginalBusiness os = new OriginalBusiness();
            return os.AddUser(converter.From(user));
        }

        public bool UpdateUser(byte[] bytes)
        {
            User user = User.ParseFrom(bytes);
            UserConverter converter = new UserConverter();

            OriginalBusiness os = new OriginalBusiness();
            return os.UpdateUser(converter.From(user));
        }

        public bool DeleteUser(int id)
        {
            OriginalBusiness os = new OriginalBusiness();
            return os.DeleteUser((short)id);
        }

        public int AddProd(byte[] bytes)
        {
            Prod prod = Prod.ParseFrom(bytes);
            ProdConverter converter = new ProdConverter();

            OriginalBusiness ob = new OriginalBusiness();
            return ob.AddProd(converter.From(prod));
        }

        public bool UpdateProd(byte[] bytes)
        {
            Prod prod = Prod.ParseFrom(bytes);
            ProdConverter converter = new ProdConverter();

            OriginalBusiness ob = new OriginalBusiness();
            return ob.UpdateProd(converter.From(prod));
        }

        public bool DeleteProd(int id)
        {
            OriginalBusiness ob = new OriginalBusiness();
            return ob.DeleteProd((short)id);
        }

        public byte[] SearchProdClass(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdClass(conditions);
        }

        public byte[] SearchProdSeries(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdSeries(conditions);
        }

        public string SearchProdSeriesToPDF(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdSeriesToPDF(conditions);
        }

        public string SearchProdSeriesToExcel(byte[] conditions)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdSeriesToExcel(conditions);
        }

        public byte[] SearchPrimaryProdInfo(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchPrimaryProdInfo((short)id);
        }

        public string SearchPrimaryProdInfoToPDF(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchPrimaryProdInfoToPDF((short)id);
        }

        public string SearchPrimaryProdInfoToExcel(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchPrimaryProdInfoToExcel((short)id);
        }

        public byte[] SearchProdInfo(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdInfo((short)id);
        }

        public string SearchProdInfoToPDF(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdInfoToPDF((short)id);
        }

        public string SearchProdInfoToExcel(int id)
        {
            SearchBusiness sb = new SearchBusiness();
            return sb.SearchProdInfoToExcel((short)id);
        }

        public byte[] SearchRecommendedProd(byte[] conditions)
        {
            return null;
        }
    }
}
